{use master_page('admin/v_master')} 

{block content}
<?php 
$cu_group = $this->usergroup_manager->get($this->current_user()->usergroup_id);  
?>
<script src="../../scripts/jquery_plugins/overlay/jquery.overlay.js"></script>  

<script src="<?=_U("script/helpers/directory")?>"></script> 
<script src="<?=_U("script/helpers/file")?>"></script>  

<script>
var file_helper = file;
$(document).ready(function(){ 
		$('.tpl_btn_edit').css('cursor','pointer')  ;  
		$('body').append( $('#mask')); 
		$('#mask').css({
			zIndex:'3000',
			opacity:0.3,
			background:'#888',
			width:"100%",
			height:"100%",
			position:"absolute" ,
			top:0,
			left:0,
			display:"none"
		}).click(function(){
			$(this).css('display','none');
			$("#edit_container").css('display','none');
		}); 

		$('#edit_container #close').click(function(){
			$('#mask').css('display','none') ;  
			$("#edit_container").css('display','none');
		}) ; 
		// alert($('#mask #edit_container').attr('id')) ; 
		$('body').append( $('#edit_container')); 
		 $('#edit_container').css({ 
			zIndex:9999,
			background:"white",
			opacity:'1',
			width:"96%",
			height:"92%",
			left:"2%",
			top:"4%",
			position:"absolute",
			display:"none"
		}); 
		
		$('.tpl_btn_edit').click(function(evt){
			$('#html').val('')  ;    
			$("#mask,#edit_container").css('display','') ;
			var ori_css = {
				'cursor':'pointer',
				'display':'inline-block',
				'width':'60px',
				'text-align':'center' ,
				'height':'20px;','line-height':'20px',
				'border-top':'1px solid white' ,'border-left':'1px solid white' ,
				'border-bottom':'1px solid #aaaaaa' ,'border-right':'1px solid #aaaaaa' ,   
				'margin-top':'2px'
			} ; 
			$('#edit_container #save').css(ori_css) ;   
			$('#edit_container #save').mousedown(function(){
				$(this).css({
					'border-bottom':'1px solid white' ,'border-right':'1px solid white' ,
					'border-top':'1px solid #aaaaaa' ,'border-left':'1px solid #aaaaaa'    
				});
			});
			$('#edit_container #save').mouseup(function(){
				$(this).css(ori_css)  ;   
			}) ;    
		
			$('#edit_container #close').css({
				'cursor':'pointer',
				'float' :'right',
				'width':"26px",
				'text-align':'center'
			}) ;    
			$('#edit_container #close').mouseover(function(){
				$(this).css('background','#fafafa');
			});
			$('#edit_container #close').mouseout(function(){
				$(this).css('background','#efefef'); 
			});
			var name = $(this).attr('name')  ;  
			var base_path = "application/views/" + name + "/front";
			// alert(base_path);
			directory.call('directory_map',base_path,function(data){
				var nodes =[];
				 var index = -1;
				function bl(file,file_name,pId,p_fullpath,level)
				{
					var pad = "";
					for(i=1;i<level;i++)
					{
						 pad +="-"; 
					}
					if(typeof(file)=="object")
					{
						var node = {};
						node.id = 0;
						node.full_path =  base_path ;   
						if(file_name !=  null && file_name !='_svn' && file_name !='images') 
						{
							index ++ ;
							node.name = file_name;
							node.full_path = p_fullpath + "/" + node.name ;  
							node.id = index + 1;
							node.pId = pId;
							node.isParent = true;
							nodes[index] = node;
						}
						if( file_name !='_svn' && file_name !='images')
						{
							$.each(file,function(name,dir){
								bl(dir,name,node.id,node.full_path,level+1);
								// bl(dir,name,node.id,"",level+1);
							});   
						}
					}
					else
					{
						index ++ ;
						var node = {};
						node.name = file;
						node.full_path = p_fullpath + "/" + node.name ;   
						node.id = index + 1;
						node.pId = pId;
						node.isParent = false;
						nodes[index] = node;
					}
				}
		
				bl(data,null,0,'',0);
		
				var setting = {
						data: {simpleData: {enable: true}},
						callback: {
							onClick: function(event, treeId, treeNode) 
							{
								if(!treeNode.isParent)
								{
									var segs = treeNode.name.split('.') ;  
									var extend_name = segs[segs.length-1] ;  
									extend_name = extend_name.toLowerCase(); 
									// alert(extend_name) ; 
									if(extend_name == "php" || extend_name == "html" || extend_name == "htm" || 
										extend_name == "css" || extend_name == "js" ) 
									{
										file_helper.call('read_file',"./"+treeNode.full_path,function(data){
											//alert(data.result);
											var html = $("<textarea name='html' id='html' style='width:100%;height:100%'></textarea>");
											$('#main_body').html('');
											$('#main_body').append(html); 
											$('#html').val(data.result)  ; 
											$('#edit_container #save').unbind('click')  ;   
											$('#edit_container #save').click(function(){
												// alert(treeNode.full_path) ;   
												file_helper.call('write_file',"./" + treeNode.full_path,$('#html').val(),function(data){ 
													if(data.result) {
														alert('保存成功') ; 
													}else{
														alert('保存失败') ; 
													}
												});
											}) ; 
										});
									}
									else if(extend_name == "jpeg" || extend_name == "jpg" || extend_name == "gif" || 
											extend_name == "png" || extend_name == "bmp" )
									{
										var img = $("<img src='../front" + treeNode.full_path.replace('application/views/'+name+'/front','') + "' id='img' style='width:100%;height:100%'></img>");
										$('#main_body').html('');
										$('#main_body').append(img);
										$('#edit_container #save').unbind('click')  ;   
										$('#edit_container #save').click(function(){
											alert("此格式不允许保存")  ; 
										});
									}
									else if(extend_name == "swf")
									{
										var src="../front" + treeNode.full_path.replace('application/views/'+name+'/front','') ;
										window.open(src)  ; 
									}
									else
									{
										alert("不支持的格式 ")  ;  
									}
								}
							}
						}
					};
					var zTreeObj ;   
		      		zTreeObj = $.fn.zTree.init($("#tree"), setting, nodes);
			})
		})
}) ; 

</script>
<style>
#edit_container .tpl_title{display:inline-block;height:26px;line-height:26px;background:#efefef;}
#mask{margin:0px;padding:0px;}
#tagscontent{margin:0px;padding:0px;}
</style>
<div id="mask" >
	<div id="edit_container" >
		<table style="width:100%;border:0px solid black">
		<tr><td style="width:230px;height:26px;background:#efefef;">
			<div class="tpl_title">模版列表</div>
		</td><td valign="top" style="background:#efefef;"> 
			 <div class="tpl_title" >
			 	<span id="close" style="margin-right:-1065px;*margin-right:0px !important">×</span> 
			 	<span id="save"> 保存 </span>
			 </div>
		</td></tr>
		<tr><td valign="top" >
			<div style="clear:both">
			<ul id="tree" class="ztree" style="border:0px solid black;width:100%;overflow:auto;"></ul>
			</div>
		</td><td valign="top" >
			 <div style="height:578px;border:1px solid  #888" id="main_body">
			 	<!--textarea name="html" id="html" style="width:100%;height:100%;border:0px"></textarea--> 
			 </div>
		</td></tr>
		</table> 
	</div>
</div>
<div style="margin-bottom:0px;">
<div class="blank20" style="height:10px;"></div>
<form id = 'form1' action = '<?=$_SERVER['REQUEST_URI']?>' method='post'>
<div class="blank5" style="height:2px;"></div>
<!-- div id="tagstitle">
	<a id="one1" onclick="setTab('one',1,20)" class="hover" href="#">模板设置</a>
</div -->
<div id="tagscontent" class="right_box" style="padding:10px;">
	<table class="datatable" >
		<thead>
			<tr>
				<th><?=lang('template_name')?></th>
				<th><?=lang('template_author')?></th>
				<th>模版名称</th>
				<th style="width:120px">操作</th>
			</tr>
		</thead>
		<tbody>
			<?php foreach($templates as $name=>$template):?>
			<tr> 
				<td align="center">{template->show_name}</td> 
				<td align="left">{template->author}</td> 
				<td align="center">{template->profile}</td> 
				<td style="width:88px" >
					<?if($template->name != $site_info->global_template):?>
					<?php if($cu_group->permission("template","apply")):?>
					<a class="unused" href="<?=_U('template/apply?name=')?>{template->name}" onmouseover="tooltip.show('启用');" onmouseout="tooltip.hide();">
						&nbsp;&nbsp;&nbsp;
					</a>
					<?endif?> 
					<?else:?>
					<a class="using" onmouseover="tooltip.show('使用中');" onmouseout="tooltip.hide();"></a>  
					<?endif?> 
					<?php if($cu_group->permission("template","edit")):?>
					<a name="{template->name}" onmouseover="tooltip.show('编辑');" onmouseout="tooltip.hide();" class="tpl_btn_edit"></a> 
					<?endif?> 
				</td>
			</tr>
			<?php endforeach;?>
			<!-- tr>   
				<td colspan='7' style="background-color:#f9fcef;padding:5px 0px;text-align:center">
					<? $this->load->view('admin/v_pager',$sp_config); ?>
				</td>
			</tr-->
		<tbody>
	</table>
</div>
</form>
</div>
{end} 